package com.cw.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cw.common.R;
import com.cw.config.JwtConfig;
import com.cw.entity.Orders;
import com.cw.entity.RunnerOrder;
import com.cw.entity.dto.OrdersDto;
import com.cw.service.OrdersService;
import com.cw.service.RunnerOrderService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;


@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private  OrdersService orderService;
    @Autowired
    private RunnerOrderService runnerOrderService;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    /**
     * 下单处理
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public  R submit(@RequestBody Orders orders,HttpServletRequest request){
        orders.setFreight(BigDecimal.valueOf(3));
        orderService.submit(orders,request);
        return  R.success("null");
    }


    /**
     * 按照时间提供最新的订单
     * @return
     */
    @GetMapping("/userPage")
    public  R userPage(Integer page, Integer pageSize, HttpServletRequest request){
        //1.获取用户id
        Long currentId = JwtConfig.getCurrentId(request);
        //2.根据id获取用户的订单信息
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(currentId!=null,Orders::getUserId,currentId);
        wrapper.orderByDesc(Orders::getCreateTime);
        //3.设置分页信息
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        Page<Orders> page1 = orderService.page(ordersPage, wrapper);



        return R.success(page1);
    }

    /**
     * 分页查询+订单号模糊查询
     * @return
     */
    @GetMapping("/page")
    public R page(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize,@RequestParam(value = "number",required = false) String number,
                         @DateTimeFormat @RequestParam(value = "startTime",required = false) String startTime, HttpServletRequest request, @DateTimeFormat @RequestParam(value = "endTime",required = false)String endTime ){
        //获取分页
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Long userId = JwtConfig.getCurrentId(request);
        //通过id获取对象
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(userID!=null,Orders::getUserId,userID);
        //创建时间大于等于startTime
        //创建时间小于等于endTime
        wrapper.ge(startTime!=null,Orders::getCreateTime,startTime).le(endTime!=null,Orders::getCreateTime,endTime);
        wrapper.eq(number!=null,Orders::getNumber,number);
        wrapper.orderByDesc(Orders::getCreateTime);
        log.info("{}",number);
      IPage<OrdersDto> ordersDtoPage= orderService.pageList(ordersPage, wrapper);
//        List<Orders> list = orderService.list(wrapper);


        return R.success(ordersDtoPage);
    }

    @PutMapping
    public R deliveryOrder(@RequestBody Orders orders){
        LambdaUpdateWrapper<Orders> eq = new LambdaUpdateWrapper<Orders>().set(Orders::getStatus, orders.getStatus()).eq(Orders::getId, orders.getId());
        this.orderService.update(eq);
        return R.success("派送成功！");
    }

    /**
     * @return
     * 骑手端： //查看可以接单的单据*
     */
    @GetMapping("/getOrderByStatus")
    public R getOrder(){
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(Orders::getStatus,2);
        return R.success(this.orderService.list(ordersLambdaQueryWrapper));
    }

    /**
     * @param dto
     *   骑手端  //接单*
     * @return
     */
    @PostMapping("/getOrder")
    public R getOrder(@RequestBody RunnerOrder dto, HttpServletRequest request){
        return R.success(this.runnerOrderService.getOrder(dto,request));
    }
    /**
     * @param dto
     * 骑手端  //确认收货*
     * @return
     */
    @PostMapping("/confirm")
    public R confirmOrder(@RequestBody RunnerOrder dto, HttpServletRequest request){
        return R.success(this.runnerOrderService.confirmOrder(dto,request));
    }
}
